package com.ciwong.epaper.modules.me.ui;

import android.view.View;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.cordva.html.BaseHtmlFragment;
import com.ciwong.epaper.modules.me.bean.Duiba;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.epaper.util.HttpRequest;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.libs.utils.volley.BaseRequest;
import com.ciwong.mobilelib.application.BaseApplication;
import com.ciwong.mobilelib.i.GoBackListener;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.ciwong.mobilelib.utils.ToastUtil;

/**
 * Created by keepon on 2018/11/23.
 */
public class FindFragment extends BaseHtmlFragment {
	private static final String TAG = "FindFragment";
	private String title ="发现";


	public static FindFragment newInstance() {
		FindFragment fragment = new FindFragment();
		return fragment;
	}

	@Override
	protected void init() {
		super.init();
		hideBackBar();
		getTitleBar().setTitle(title);
		getTitleBar().showRightTextAndTextDrawable("积分乐园",mActivity.getResources().getDrawable(R.mipmap.icon_integral_paradise));
		setGoBackListener(new GoBackListener() {
			@Override
			public void goBack() {
				if(getCordovaWebView()!=null){
					getCordovaWebView().backHistory();
					if (!getCordovaWebView().canGoBack()){
						hideBackBar();
					}
				}
			}
		});
		BaseRequest.VerifyInfo verifyInfo = HttpRequest.getVerifyInfo();
		if (verifyInfo != null) {
			String clientId = verifyInfo.getClientId();
			String userId = EApplication.getInstance().getUserInfoBase().getUserId() + "";
			String brandId = EApplication.BRAND_ID + "";
			String startURL = EpaperConstant.URL_H5_FIND +"clientId=" + clientId +  "&userId=" + userId + "&brandId=" + brandId;
			setStartURL(startURL);
			loadUrl(startURL.toString());
		} else {
			showToastError("无法获取用户信息，请重新登录后重试");
		}
	}


	@Override
	protected void loadData() {
	}

	@Override
	protected void initEvent() {
		super.initEvent();
		getTitleBar().setRightBtnListener(new ViewOnClickListener() {
			@Override
			public void avertRepeatOnClick(View v) {
				try {
					if(NetworkUtils.isOnline()){
						int userId = (int) BaseApplication.getInstance().getUserInfoBase().getUserId();
						MeDao.getInstance().getDuibaLogin(userId,null,new BaseExtCallBack(mActivity,userId+""){
							@Override
							public void failed(Object data) {
								super.failed(data);
								ToastUtil.INSTANCE.toastCenterError(R.string.server_error);


							}

							@Override
							public void failed(int errorCode, Object data) {
								super.failed(errorCode, data);
								if (errorCode == 5201) {
									ToastUtil.INSTANCE.toastCenterErrorWithErrorCode(data.toString(), errorCode);
								} else {
									if (null != data && errorCode != 17&&errorCode!=27) {
										ToastUtil.INSTANCE.toastCenterNoNetErrorOrServerError();
									}
								}
							}

							@Override
							public void success(Object data) {
								if(data !=null&&data instanceof Duiba){
									Duiba duiba = (Duiba) data;
									MeJumpManager.jumpToDuiBaActivity(mActivity,R.string.go_back,duiba.getDuiBaUrl());
								}else{
									ToastUtil.INSTANCE.toastCenterError(R.string.server_error);
								}
							}
						});
					}else{
						ToastUtil.INSTANCE.toastCenterNoNetError();
					}
				}catch (Exception e){

				}


			}
		});
	}











}
